import Md5 from 'js-md5'
import { Encrypt } from '../assets/js/utils.js'
// import { callHanlder, connectBridge } from './JSBridge'

export default {
  data: () => ({
    deviceType: '',
    mobileHeight: null,
    topNum: 0,
    encryption: 'cn2u39dbcnqiowap',
    userId: '',
    shareLink: {
      title: '',
      desc: '',
      imgUrl: '',
      url: ''
    },
    currentExportUrl: 'https://cpcrm.cpgroup.cn'
    // currentExportUrl: 'https://precrm.cpgroup.cn'
  }),
  created () {
    // let userid = document.cookie.match(/AbcfN_ajaxuid=([^;$]+)/)
    // this.userId = userid ? userid[1] : ''
    // this.deviceType = this.getType()
    // this.mobileHeight = this.getMobileHeight()
  },
  computed: {},
  methods: {
    // md5加密
    encryptionJson (json) {
      let newObj = {}
      json.timestamp = JSON.stringify(Date.parse(new Date()) / 1000)
      // console.log(Date.parse(new Date()) / 1000)
      let arg = Encrypt(json)
      let keyArraySort = Object.keys(json).sort()
      for (var i = 0; i < keyArraySort.length; i++) {
        let k = keyArraySort[i]
        newObj[k] = json[k]
      }
      let data = Md5(`${JSON.stringify(newObj)}${this.encryption}`)
      let obj = {
        arg: arg,
        token: data,
        timestamp: json.timestamp
      }
      return obj
    },
    showTipInfo (msg) {
      this.$showToast.show({
        text: msg,
        type: 'error',
        time: 1500
      })
    },
    getCookie (name) {
      // name 为想要取到的键值的键名
      let reg = /\s/g
      let result = document.cookie.replace(reg, '')
      let resultArr = result.split(';')
      for (let i = 0; i < resultArr.length; i++) {
        let nameArr = resultArr[i].split('=')
        if (nameArr[0] === name) {
          return nameArr[1]
        }
      }
    },
    setCookie (objName, objValue, objDays) {
      var str = objName + '=' + escape(objValue)
      if (objDays > 0) {
        var date = new Date()
        var ms = objDays * 24 * 3600 * 1000
        date.setTime(date.getTime() + ms)
        str += '; expires=' + date.toGMTString()
      }
      if (objDays === Infinity) {
        str += '; expires=Fri, 31 Dec 9999 23:59:59 GMT'
      }

      str += '; path=/'
      document.cookie = str
    },
    delCookie (name) {
      var exp = new Date()
      exp.setTime(exp.getTime() - 1)
      var cval = this.getCookie(name)
      if (cval != null) { document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString() }
    },
    checkEmojiSpecialCharacters (val) {
      // let emojiReg = [
      //   '\ud83c[\udf00-\udfff]',
      //   '\ud83d[\udc00-\ude4f]',
      //   '\ud83d[\ude80-\udeff]'
      // ]
      /* eslint-disable */
      let emojiReg = /[\xf0-\xf7].{3}/
      let specialReg = /\|\/|\^|\{|\}|\<|\>|\?|\/|\`|\=|\\\|\|/
      if (emojiReg.test(val) || specialReg.test(val)) {
        return true
      } else {
        return false
      }
      /* eslint-enable */
    },
    getMobileHeight () {
      return document.documentElement.clientHeight || document.documentElement.window.innerHeight
    },
    toggleBody (isPin) {
      if (isPin) {
        this.topNum = window.scrollY || window.pageYOffset
        document.body.style.position = 'fixed'
        document.body.style.top = -this.topNum + 'px'
      } else {
        document.body.style.position = 'inherit'
        document.body.style.top = ''
        window.scrollTo(0, this.topNum)
      }
    },
    // ios微信失去焦点问题
    onBlur (e) {
      let timer = setTimeout(() => {
        const scrollHeight =
          document.documentElement.scrollTop || document.body.scrollTop || 0
        window.scrollTo(0, Math.max(scrollHeight - 1, 0))
        timer && clearTimeout(timer)
      }, 100)
    },
    GetQueryString (name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
      var r = window.location.search.substr(1).match(reg)
      if (r != null) return unescape(r[2]); return null
    }
  }
}
